package com.itheima.project.mainPage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.itheima.project.R;

import java.util.List;


public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContect;
    private List<RecyclerViewEntity.DataBean.DatasBean> mList;
    private View mBannerView;

    private final static int BANNER_VIEW_TYPE = 0;
    private final static int ITEM_VIEW_TYPE = 1;

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return BANNER_VIEW_TYPE;
        } else {
            return ITEM_VIEW_TYPE;
        }
    }


    public RecyclerViewAdapter(Context mContect, List<RecyclerViewEntity.DataBean.DatasBean> mList,View bannerView) {
        this.mContect = mContect;
        this.mList = mList;
        this.mBannerView = bannerView;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        switch (viewType){
            case BANNER_VIEW_TYPE:
                return new BannerViewHolder(mBannerView);
            case   ITEM_VIEW_TYPE:
                View inflate = LayoutInflater.from(mContect).inflate(R.layout.recyclerview_item, parent, false);
                return new RecyclerViewHolder(inflate);
        }
        return  null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == BANNER_VIEW_TYPE) {

        }else  if (itemViewType == ITEM_VIEW_TYPE){
            RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;
            RecyclerViewEntity.DataBean.DatasBean bean = mList.get(position-1);
            recyclerViewHolder.tvAuthor.setText(bean.getAuthor());
            recyclerViewHolder.tvSort.setText(bean.getChapterName()+"/"+bean.getSuperChapterName());
            recyclerViewHolder.tvCntent.setText(bean.getTitle());
            recyclerViewHolder.tvTime.setText(bean.getNiceShareDate());
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class  RecyclerViewHolder extends RecyclerView.ViewHolder{
        private TextView tvAuthor;
        private TextView tvSort;
        private TextView tvCntent;
        private TextView tvTime;
        public RecyclerViewHolder(@NonNull View itemView) {
            super(itemView);
            tvAuthor = itemView.findViewById(R.id.tv_author_recyclerview_item);
            tvSort = itemView.findViewById(R.id.tv_sort_recyclerview_item);
            tvCntent = itemView.findViewById(R.id.tv_content_recyclerview_item);
            tvTime = itemView.findViewById(R.id.tv_time_recyclerview_item);
        }
    }
    static class BannerViewHolder extends RecyclerView.ViewHolder{

        public BannerViewHolder(View itemView) {
            super(itemView);
        }
    }
}
